package org.example.service.impl;

import org.example.Mapper.EmailRecordMapper;
import org.example.Mapper.UserMapper;
import org.example.domain.EmailRecord;
import org.example.domain.User;
import org.example.service.EmailRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class EmailRecordServiceImpl implements EmailRecordService {

    @Autowired
    private EmailRecordMapper emailRecordMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addEmailRecord(EmailRecord emailRecord) {//新增一条邮箱来往
        emailRecordMapper.addEmailRecord(emailRecord.getUserId(),new Date(),emailRecord.getAskContent());
    }

    @Override
    public EmailRecord[] getEmailRecordsByUserId(Integer userId, Integer start) {
        return emailRecordMapper.getEmailRecordsByUserId(userId,start);
    }

    @Override
    public EmailRecord[] getEmailRecordsByAdminIdZero(Integer start) {
        EmailRecord[] emailRecords=emailRecordMapper.getEmailRecordsByAdminIdZero(start);
        for (EmailRecord emailRecord : emailRecords) {
            User user=userMapper.getAllById(emailRecord.getUserId());
            emailRecord.setUser(user);
        }
        return emailRecords;
    }

    @Override
    public EmailRecord[] getEmailRecordsByAdminIdNotZero(Integer start) {
        EmailRecord[] emailRecords=emailRecordMapper.getEmailRecordsByAdminIdNotZero(start);
        for (EmailRecord emailRecord : emailRecords) {
            User user=userMapper.getAllById(emailRecord.getUserId());
            emailRecord.setUser(user);
        }
        return emailRecords;
    }

    @Override
    public void replyEmailRecordById(EmailRecord emailRecord) {//管理员回复问题
        emailRecordMapper.replyEmailRecordById(emailRecord.getId(),emailRecord.getAdminId(),
                emailRecord.getAnswerContent(),new Date());
    }
}
